package cn.signalpha.modules.shop.service;

import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.enums.StatusEnum;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.log.exception.ServiceException;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.base.dto.NoticeDTO;
import cn.signalpha.modules.base.entity.enums.NoticeTypeEnum;
import cn.signalpha.modules.com.entity.User;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.queue.enums.RedisDelayQueueEnum;
import cn.signalpha.modules.queue.utils.RedisDelayQueueUtil;
import cn.signalpha.modules.shop.dto.PlaceApplyDTO;
import cn.signalpha.modules.shop.entity.PlaceApply;
import cn.signalpha.modules.shop.entity.PlaceLevel;
import cn.signalpha.modules.shop.mapper.PlaceApplyMapper;
import cn.signalpha.modules.shop.vo.PlaceApplyVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
@AllArgsConstructor
public class PlaceApplyService extends ServiceImpl<PlaceApplyMapper, PlaceApply> {

	private final UserService userService;
	private final SellerService sellerService;
	private final PlaceLevelService placeLevelService;
	private final RedisDelayQueueUtil redisDelayQueueUtil;

	/**
	 * 自定义分页
	 */
    public IPage<PlaceApplyVO> selectPlaceApplyPage(IPage<PlaceApplyVO> page, Map<String, Object> params) {
        return page.setRecords(baseMapper.selectPlaceApplyPage(page, params));
    }

    /**
	 * 自定义Wrappers
	 */
    public QueryWrapper<PlaceApply> selectPlaceApplyWrapper(Map<String, Object> params) {
        QueryWrapper<PlaceApply> wrapper = Wrappers.query();
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("userId")), PlaceApply::getUserId, params.get("userId"));
        wrapper.lambda().like(ObjectUtil.isNotEmpty(params.get("name")), PlaceApply::getName, params.get("name"));
        wrapper.lambda().like(ObjectUtil.isNotEmpty(params.get("mobile")), PlaceApply::getMobile, params.get("mobile"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("status")), PlaceApply::getStatus, params.get("status"));
        wrapper.lambda().orderByDesc(PlaceApply::getId);
        return wrapper;
    }

	/**
	 * 申请审核
	 */
	@Transactional(rollbackFor = Exception.class)
	public void placeApplyAudit(Long id, PlaceApplyDTO.AuditDTO auditDTO) {
		PlaceApply placeApply = baseMapper.selectById(id);
		Assert.isNull(placeApply, MsgConstant.OBJECT_NULL);
		Assert.isTrue(placeApply.getStatus() == 0, "请不要重复审核");
		updateById(PlaceApply.builder().id(id).status(auditDTO.getStatus()).remark(auditDTO.getRemark()).build());

		if(auditDTO.getStatus() == 1){
			PlaceLevel placeLevel = placeLevelService.getByLevel(1);
			if(ObjectUtil.isEmpty(placeLevel)){
				throw new ServiceException("请先设置分销等级");
			}
			User user = userService.getById(placeApply.getUserId());
			if(user.getIsPlace() == 1){
				throw new ServiceException("该用户已是分销员");
			}
			userService.updateById(User.builder().id(placeApply.getUserId()).isPlace(1).placeLevel(1).placeStatus(StatusEnum.ON.getType()).build());
		}

		//推送消息
		NoticeDTO noticeDTO = new NoticeDTO();
		noticeDTO.setTargetId(placeApply.getId());
		noticeDTO.setType(NoticeTypeEnum.PLACE_AUDIT.getType());
		redisDelayQueueUtil.addDelayQueue(noticeDTO, 1, RedisDelayQueueEnum.NOTICE.getCode());
	}
}

